/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.common.ReflectUtils;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.domain.ReplyInfo;
import com.fujitsu.nanjing.tieba.domain.TopicInfo;
import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.ibator.CoreTopicCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;
/**
 * TopicInfoService.java
 * 
 * @author Qixiaobo-win8
 */

@Service
@Transactional
public class TopicInfoService extends BaseService<TopicInfo, CoreTopicCriteria> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(TopicInfoService.class);

	@Autowired
	private TopicService topicService;
	@Autowired
	private ReplyInfoService replyInfoService;
	@Autowired
	private UserService userService;
	@Autowired
	private AttachService attachService;
	@Autowired
	private BoardService boardService;
	@Override
	public TopicInfo findById(String id) {
		return findByIdWithPageNum(id, CommonParamter.起始页码);
	}
	@Override
	public TopicInfo insert(TopicInfo t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(TopicInfo t) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(TopicInfo t) {
		// TODO Auto-generated method stub

	}
	public TopicInfo findByIdWithPageNum(String id, int pageNum) {
		CoreTopic coreTopic = topicService.findById(id);
		if (coreTopic == null)
			return null;
		TopicInfo info = new TopicInfo();
		try {
			ReflectUtils.copy(coreTopic, info);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		// TODO 基本上可以在此处加上缓存
		CoreBoard coreBoard = boardService.findById(info.getBoardid()
				.toString());
		info.setBoardName(coreBoard.getBoardname());
		CoreUser coreUser = userService.findById(info.getUserid());
		info.setUser(coreUser);
		info.setOffset(pageNum);
		info.setLength(CommonParamter.默认分页长度);
		info.setTotalCount(topicService.countByBoardId(info.getBoardid()));
		info.setReplyInfos(replyInfoService.findReplyInfosByTopicIdDefault(
				info.getTopicid(), info.getOffset()));
		info.setAttachInfos(attachService.getAllAttachesByTopicId(info
				.getTopicid()));
		info.setPage((int) Math.ceil((double) info.getReplies()
				/ info.getLength()));
		return info;
	}
	/**
	 * 当返回正常页数或-1
	 **/
	public int findPageIndexWithReplyId(String topicId, String replyId) {
		int pages = topicService.findPageCount(topicId);
		for (int i = 1; i <= pages; i++) {
			TopicInfo info = findByIdWithPageNum(topicId, i);
			for (ReplyInfo reply : info.getReplyInfos()) {
				if (reply.getReplyid().equals(Integer.parseInt(replyId))) {
					return i;
				}
			}
		}
		return -1;
	}
}
